﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;

namespace EasyLibrary.AR.Classes
{
  public class RotaViatura:ActiveRecord
    {
      public int IdRotaViatura { get; set; }
      public int IdViatura { get; set; }
      public int IdRota { get; set; }
      public DateTime DataIni { get; set; }
      public DateTime DataFim { get; set; }
      public bool Activo { get; set; }
      public DateTime DataInsercao { get; set; }

      public RotaViatura() { }

      public RotaViatura(DateTime di,DateTime df)
      {
          this.DataIni = di;
          this.DataFim = df;
      }

      public RotaViatura GetDatasByRotaAndViatura(int idVia,int idRota)
      {
          try
          {
   
             DataSet ds = executeCommandOnDataSet(string.Format(CultureInfo.InvariantCulture, "Select DataIni,DataFim from rotasviatura where IdViatura={0} and IDRota={1}",idVia,idRota));

              //IList ret = new ArrayList();
             RotaViatura rv = new RotaViatura(DataIni, DataFim);
              foreach (DataRow r in ds.Tables[0].Rows)
              {
                
                  rv.DataIni = (DateTime) r["DataIni"];
                  rv.DataFim = (DateTime) r["DataFim"];

              }

              return rv;
          }
          catch (Exception ex)
          {
              throw new ApplicationException("Erro BD", ex);
          }
      }

      public override void Save()
      {
          throw new NotImplementedException();
      }

      public int getIDRotaViaturaByRotaAndViatura(int idRota, int idViatura)
      {
          try
          {

              DataSet ds = executeCommandOnDataSet(string.Format(CultureInfo.InvariantCulture, "select IdRotaViatura from RotasViatura where IDRota={0} and IDViatura={1} and Activo={2}", idRota, idViatura,1));

              //IList ret = new ArrayList();
              int rv = -1;
             // foreach (DataRow r in ds.Tables[0].Rows)
             // {
                  rv=(int)ds.Tables[0].Rows[0]["IdRotaViatura"];
                //  rv=(int) r["IdRotaViatura"];
               //   break;
                  // rv.DataIni = (DateTime)r["DataIni"];
                  // rv.DataFim = (DateTime)r["DataFim"];

             // }

              return rv;
          }
          catch (Exception ex)
          {
              throw new ApplicationException("Erro BD", ex);
          }
      }

      public int getIdViaturaByIdRotaViatura(int idRotaViatura)
      {
          int ret;

          try
          {

              DataSet ds = executeCommandOnDataSet(string.Format(CultureInfo.InvariantCulture, "select IdViatura from RotasViatura where IDRotaViatura={0}", idRotaViatura));

              //IList ret = new ArrayList();
            //  int rv = -1;
              // foreach (DataRow r in ds.Tables[0].Rows)
              // {
              ret = (int)ds.Tables[0].Rows[0]["IdViatura"];
              //  rv=(int) r["IdRotaViatura"];
              //   break;
              // rv.DataIni = (DateTime)r["DataIni"];
              // rv.DataFim = (DateTime)r["DataFim"];

              // }

              return ret;
          }
          catch (Exception ex)
          {
              throw new ApplicationException("Erro BD", ex);
          }

          return ret;

      }
    }

  
}
